package com.example.yrdev.ordering_system.Main_Menu;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.example.yrdev.ordering_system.Adapter.GridViewAdapter;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.Common.Setting;
import com.example.yrdev.ordering_system.Dialog.InputPassDialog;
import com.example.yrdev.ordering_system.Dialog.InternetSettingDialog;
import com.example.yrdev.ordering_system.Menu_Setting.SettingInternetActivity;
import com.example.yrdev.ordering_system.Menu_Setting.SettingLanguageActivity;
import com.example.yrdev.ordering_system.Menu_Setting.SettingOperatorDialog;
import com.example.yrdev.ordering_system.Model.CardRecord;
import com.example.yrdev.ordering_system.Model.Good;
import com.example.yrdev.ordering_system.Model.PurchaseReport;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.User_Login_Register.LoginActivity;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by yrdev on 8/18/16.
 */
public class MenuSettingActivity extends CommonActivity {

    @InjectView(R.id.option_menu)GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showBackButton();
        setBarTitle(getResources().getString(R.string.setting));
        initView();

    }

    public void  initView(){


        setContentView(R.layout.main_menu_layout);
        ButterKnife.inject(this);

        Integer [] images={R.drawable.setting_sale_type,
                R.drawable.setting_print,R.drawable.setting_internet,R.drawable.setting_init,R.drawable.setting_language};
        String[] titles={"消费模式设置","打印功能设置","网络设置","初始化",getResources().getString(R.string.language)};


        gridView.setAdapter(new GridViewAdapter(this, images,titles));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                switch (view.getId()) {


                    case R.drawable.setting_sale_type:
                        settingWorkingMode();
                        break;

                    case R.drawable.setting_communication:

                        break;

                    case R.drawable.setting_print:

                        settingPrinter();

                        break;


                    case R.drawable.setting_internet:

                        settingIntenet();

                        break;


                    case R.drawable.setting_init:
                        settingInit();
                        break;


                    case R.drawable.setting_language:
                        gotoActivity(SettingLanguageActivity.class,0);
                        activatePushingAnim();

                        break;


                }
            }
        });


    }


    public void settingOperator(){

        final AlertDialog.Builder builder= new AlertDialog.Builder(this);
        final String[] dialogItems={"添加操作员","删除操作员","修改操作员密码"};

        builder.setTitle("操作员设置").setItems(dialogItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                SettingOperatorDialog operatorDialog=new SettingOperatorDialog(MenuSettingActivity.this,which,dialogItems[which]);
                operatorDialog.show();
            }
        });
        builder.show();

    }

    private void settingInit(){
        final InputPassDialog passDialog=new InputPassDialog(this);
        passDialog.setCallback(new InputPassDialog.Callback() {
            @Override
            public void grantAccess() {
            passDialog.dismiss();
                showInitOption();
            }
        });
        passDialog.show();




    }

    int index=0;
    private void showInitOption(){

        final AlertDialog.Builder builder= new AlertDialog.Builder(this);
        final String[] dialogItems={"清空商品消费记录","清空商品信息"};


        builder.setTitle("初始化").setItems(dialogItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, final int which) {
                index=which;

               AlertDialog.Builder temp=new AlertDialog.Builder(MenuSettingActivity.this);
                temp.setTitle("提示");
                temp.setMessage("你是否确定"+dialogItems[which]+"?");
                temp.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        switch (index){
                            case 0:

                                PurchaseReport.removeAllreport(MenuSettingActivity.this);

                                break;
                            case 1:

                                Good.removeAllGood(MenuSettingActivity.this);

                                break;

                        }

                        GlobalFunction.ToastAlert(MenuSettingActivity.this,"清空成功");
                        dialogInterface.dismiss();

                    }
                });
                temp.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                temp.show();


            }
        });
        builder.show();
    }

    private void settingPrinter(){
        final AlertDialog.Builder builder= new AlertDialog.Builder(this);
        final String[] dialogItems={"不打印","打印"};

        builder.setTitle("打印功能设置").setItems(dialogItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0:
                        GlobalFunction.ToastAlert(MenuSettingActivity.this,"关闭打印模式");
                        break;
                    case 1:
                        GlobalFunction.ToastAlert(MenuSettingActivity.this,"开启打印模式");
                        break;
                }
                Setting.getInstance().assignPrintingMode(which);

            }
        });
        builder.show();

    }


    private void settingWorkingMode(){
        final AlertDialog.Builder builder= new AlertDialog.Builder(this);
        final String[] dialogItems=Setting.reportModeList;


        builder.setTitle("工作模式").setItems(dialogItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                GlobalFunction.ToastAlert(MenuSettingActivity.this,"您选择"+dialogItems[which]+"工作模式");
                Setting.getInstance().setSellerType(which);
            }
        });
        builder.show();

    }

    private void settingIntenet(){

        InternetSettingDialog internetSettingDialog=new InternetSettingDialog(this);
        internetSettingDialog.show();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       restart();

    }
}
